package com.hnucm.springboot.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Controller
public class LoginController {

    @RequestMapping("/uploadcommit")
    public String upload(MultipartFile file){
        // 保存文件
        File file1 = new File("D:/data/");
        //随机数 不重复 永远
        String fileName = UUID.randomUUID().toString() +file.getOriginalFilename();
        try {
            file.transferTo(new File(file1,fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "sucess.html";
    }


// <!--https://news.hnucm.edu.cn/info/1012/15901.htm-->
// <!--https://news.hnucm.edu.cn/info/1012/15899.htm-->
    @RequestMapping("/news/{newsid}")
    public String news(@PathVariable String newsid,Model model){
        //todo -> 根据新闻id查询数据库查询新闻的数据
        model.addAttribute("newsid",newsid);
        return "news.html";
    }

    //热更新
    @RequestMapping("/login")
    public String login(){
        return "login.html";
    }

    @RequestMapping("/logincommit")
    public String logincommit(String username, String password, Model model){
        //todo 数据库查询登录验证
        // 数据库  ->MySQL 8.0

        if (username.equals("admin")&&password.equals("123456")){
            model.addAttribute("username",username);
            return "main.html";
        }

        return "fail.html";
    }



}
